package com.leizhi.demo.monitor.pojo;

import com.leizhi.demo.monitor.utils.SipConstantUtil;
import io.netty.channel.Channel;
import lombok.Data;

import java.io.IOException;
import java.io.OutputStream;

/**
 * @Description 推流进程信息
 * @Version 1.0.0-SNAPSHOT
 * @Author leizhi
 * @Date 2025/5/10 14:15
 */
@Data
public class PushStreamProcessInfo {

    /* 设备ID */
    private String deviceId;

    /* 推流 SSRC */
    private Long ssrc;

    /* 推流端口 */
    private Integer port;

    /* 流媒体传输协议 */
    private String flowProtocol;

    /* RTP 通道 */
    private Channel channel;

    /* FFmpeg 进程 */
    private Process ffmpegProcess;

    /* FFmpeg 输入流 */
    private OutputStream ffmpegInput;

    /* 是否关闭 FFmpeg 输入流 */
    private boolean isFfmpegInputClosed;

    /* 是否云存储 */
    private boolean isCloud;

    /* 是否有新的数据包 */
    private boolean isNewPayload;


    public PushStreamProcessInfo(String deviceId, Long ssrc, Boolean isCloud, Integer port) {
        this.deviceId = deviceId;
        this.ssrc = ssrc;
        this.port = port;
        this.isCloud = isCloud;
        this.isNewPayload = false;
        this.isFfmpegInputClosed = true;
    }

    public void setProcessInfo(Process ffmpegProcess, OutputStream ffmpegInput) {
        this.ffmpegProcess = ffmpegProcess;
        this.ffmpegInput = ffmpegInput;
        this.flowProtocol = SipConstantUtil.UDP;
        this.isFfmpegInputClosed = false;
    }

    public void setProcessInfo(Process ffmpegProcess, OutputStream ffmpegInput, Channel channel) {
        this.ffmpegProcess = ffmpegProcess;
        this.ffmpegInput = ffmpegInput;
        this.channel = channel;
        this.flowProtocol = SipConstantUtil.TCP;
        this.isFfmpegInputClosed = false;
    }

    public void close() throws IOException {
        if (channel != null) {
            channel.close();
        }

        if (ffmpegInput != null) {
            ffmpegInput.flush();
        }

        if (ffmpegProcess != null) {
            ffmpegProcess.destroy();
        }
    }

    public void flush() throws IOException {
        if (ffmpegInput != null) {
            ffmpegInput.flush();
        }
    }

}
